import { createStore } from "vuex";
import {
    getGoodsList,
    getDetails,
    getDetailsIntroduce,
    getCartData,
    getClassifyGoods,
} from "@/api/product.js";
// 导入获取地址的 API
import { getAddress } from "@/api/address";
// 导入获取用户信息的 API
import { getUserInfo } from "@/api/user";
// 导入递归函数
import { changeAddress } from "@/utils/tool";

export default createStore({
    state: {
        // 商品列表数据
        goodsList: [],
        // 商品详情轮播图
        figureList: [],
        // 商品详情介绍
        introduceImg: [],
        // 商品详情参数弹出层
        show: { showRound: false },
        // 购物车数据
        cartList: [],
        // 商品分类数据
        classifyGoods: [],
        // 地址列表
        cascaderColumns: [],
        // 用户信息
        userInfo: {},
        // 有搜索结果的商品列表
        foundProductList: [],
        // 订单id
        orderId: [],
        // 订单详情页的订单 id
        orderDetailId: "",
    },
    getters: {},
    mutations: {
        // 商品详情页弹出层
        show(state) {
            state.show.showRound = true;
        },
        // 将商品列表保存goodsList中
        setGoodsList(state, payload) {
            state.goodsList = payload;
        },
        // 将详情页轮播图保存在figureList中
        setDetailsData(state, payload) {
            let arr = payload;
            state.figureList = [];
            arr.forEach((e) => {
                state.figureList.push(e.s_photos[0].path);
            });
        },
        // 储存详情页商品介绍图片到introduceImg
        setDetailsIntroduceData(state, payload) {
            let arr = payload;
            state.introduceImg = [];
            arr.forEach((e) => {
                state.introduceImg.push(e.s_photos[0].path);
            });
        },
        // 储存购物车数据
        setShopCartData(state, payload) {
            state.cartList = payload;
            console.log(state.cartList);
        },
        // 储存商品分类数据
        setClassify(state, payload) {
            state.classifyGoods = payload;
        },
        // 储存地址列表
        setAddressList(state, payload) {
            state.cascaderColumns = payload;
        },
        // 储存用户信息
        setUserInfo(state, payload) {
            state.userInfo = payload;
            console.log(state.userInfo);
        },
        // 储存搜索结果的商品
        setFoundProductList(state, payload) {
            state.foundProductList = payload;
        },
        // 添加订单id
        addOrderId(state, payload) {
            state.orderId.push(payload);
        },
        // 暂存订单详情页的 id
        setOrderDetailId(state, payload) {
            state.orderDetailId = payload;
            console.log(state.orderDetailId);
        },
    },
    actions: {
        // 请求商品列表
        getGoodsListData(context) {
            getGoodsList()
                .then((res) => {
                    console.log(res);
                    context.commit("setGoodsList", res.data.result.rows);
                })
                .catch((err) => {
                    console.log(err);
                });
        },

        // 请求商品详情页轮播图
        getDetailsData(context) {
            getDetails()
                .then((res) => {
                    context.commit("setDetailsData", res.data.result);
                })
                .catch((err) => {
                    console.log(err);
                });
        },
        // 请求商品详情介绍图
        getDetailsIntroduceData(context) {
            getDetailsIntroduce()
                .then((res) => {
                    context.commit("setDetailsIntroduceData", res.data.result);
                })
                .catch((err) => {
                    console.log(err);
                });
        },
        // 请求购物车数据
        getShopCartData(context) {
            getCartData()
                .then((res) => {
                    context.commit("setShopCartData", res.data.result);
                })
                .catch((err) => {
                    console.log(err);
                });
        },
        // 获取商品分类列表
        async getClassify(context) {
            const { data } = await getClassifyGoods();
            context.commit("setClassify", data.result);
        },
        // 获取地址列表
        async getAddressList(context) {
            const { data } = await getAddress();
            context.commit("setAddressList", changeAddress(data.result));
        },
        // 获取用户信息
        async getUserInfo(context) {
            const { data } = await getUserInfo();
            context.commit("setUserInfo", data.result);
        },
    },
});
